<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once BASE_PATH.'application/controllers/base'.EXT;

class Contacto extends Base {
	public function __construct() {
        parent::__construct();
        $this->load->library('email');
        $this->load->library('form_validation');
    } 

    public function index() {
        $this->load->view('contacto/contacto');
    }

    public function process() {

        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');

        $this->form_validation->set_rules('name', 'Nombre', 'trim|required|alpha|min_length[3]|max_length[15]');
        $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
		$this->form_validation->set_rules('tema', 'Tema', 'required');
        $this->form_validation->set_rules('mensaje', 'Mensaje', 'required');

        #Mensajes personalizados
        $this->form_validation->set_message('min_length', 'Minimum length for %s is %s characters');
        $this->form_validation->set_message('max_length', 'Maximum length for %s is %s characters');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('contacto/contacto');
        } else {
            //Form validation is successful, clean the input and use it.
            //For example store in db (In this tutorial we wont use database).
            $this->name = $this->security->xss_clean($this->input->post('name'));
			$this->apellido = $this->security->xss_clean($this->input->post('apellido'));
            $this->email = $this->security->xss_clean($this->input->post('email'));
			$this->telefono = $this->security->xss_clean($this->input->post('telefono'));
			$this->tema = $this->security->xss_clean($this->input->post('tema'));
			$this->pais = $this->security->xss_clean($this->input->post('pais'));
			$this->entidad = $this->security->xss_clean($this->input->post('entidad'));
            $this->mensaje = $this->security->xss_clean($this->input->post('mensaje'));

            #Since we are not storing it to database, 
            #lets send this data to our success view to display there.          
            $data['name'] = $this->name;
			$data['apellido']=$this->apellido;
            $data['email'] = $this->email;
			$data['telefono'] = $this->telefono;
			$data['tema'] = $this->tema;
			$data['pais'] = $this->pais;
			$data['entidad'] = $this->entidad;
            $data['mensaje'] = $this->mensaje;
			
			 

            #cargar la vista success con los datos.
           $this->load->vars($data);
            mail("suscripciones@newsweek.mx", $data['tema'], 
			"Nombre: ".$data['name']." ".$data['apellido']."<br>". 
			"Tema: ".$data['tema']."<br>".
			"Telefono: ".$data['telefono']."<br>".
			"Pais: ".$data['pais']."<br>".
			"Entidad: ".$data['entidad']."<br>".
			"Mensaje: ".$data['mensaje']);
			
            $this->load->view('contacto/success',$data);
            
            //$data = array("nombre" => $this->name, "email" => $this->email);
            
            //redirect("./contacto/enviaremail");   
            //$this->enviaremail($data);
        } 
    } 

    public function enviaremail($data) {
        
        $this->email->set_newline("\r\n");

        $this->email->from($data['email'], $data['name'].' '.$data['apellido']);
        $this->email->to('suscripciones@newsweek.mx');
        $this->email->subject($data['tema']);
        $this->email->message(
		"Nombre: ".$data['name']." ".$data['apellido']."<br>". 
		"Tema: ".$data['tema']."<br>".
		"Telefono: ".$data['telefono']."<br>".
		"Pais: ".$data['pais']."<br>".
		"Entidad: ".$data['entidad']."<br>".
		"Mensaje: ".$data['mensaje']);  


        if ($this->email->send()) {
            $this->load->view('contacto/success',$data);
            //echo 'Se ha enviado el mensaje correctamente.';
        } else {
            show_error($this->email->print_debugger());	
        }
    }

}